#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')),
                                        hardened_gcc => 0 }) %]
[% IF c("var/linux-cross") -%]
  [% pc('gcc', 'var/setup', {
    compiler_tarfile => c('input_files_by_name/' _ 'gcc-host'),
    hardened_gcc => 0,
    target => [ c('var/channel'), 'torbrowser-linux-x86_64' ]
  }) %]

  # GCC's cc1 expects Jessie libmpfr; we feed it a symlink to Buster's instead
  # as a stupid compat cludge.  TODO: Maybe copy in the actual Jessie version
  # from the GCC build container?
  mkdir -p /var/tmp/dist/libmpfr
  ln -s -T /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /var/tmp/dist/libmpfr/libmpfr.so.4
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/var/tmp/dist/libmpfr"
[% END -%]
distdir=/var/tmp/dist/[% project %]
mkdir -p /var/tmp/build

[% IF c("var/android") %]
  TORCONFIGDIR="$distdir/assets/common"
  libsdir=$distdir/jniLibs/[% c("var/abi") %]
  mkdir -p $libsdir $TORCONFIGDIR "$distdir/Tor"
  # We need to make sure our ZSTD dependencey is properly picked up.
  # --enable-zstd is not enough.
  export ZSTD_CFLAGS=-I/var/tmp/dist/zstd/include
  export ZSTD_LIBS=/var/tmp/dist/zstd/lib/libzstd.a
[% END %]

[% IF c("var/linux") || c("var/windows") %]
  TORCONFIGDIR="$distdir/Data/Tor"
  mkdir -p $TORCONFIGDIR
  mkdir -p "$distdir/Tor"
[% END %]

[% IF c("var/osx") %]
  TORBINDIR="$distdir/Contents/MacOS/Tor"
  TORCONFIGDIR="$distdir/Contents/Resources/TorBrowser/Tor"
  mkdir -p $distdir/bin
  mkdir -p $TORBINDIR
  mkdir -p $TORCONFIGDIR
[% END %]

[% IF c("var/windows") || c("var/android") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/zlib') %]
  zlibdir=/var/tmp/dist/zlib
[% END %]
[% IF c("var/android") %]
  tar -C /var/tmp/dist -xf [% c('input_files_by_name/zstd') %]
[% END %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/openssl') %]
tar -C /var/tmp/dist -xf [% c('input_files_by_name/libevent') %]
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz
libeventdir=/var/tmp/dist/libevent
openssldir=/var/tmp/dist/openssl/openssl

[% IF c("var/windows") %]
  # XXX: It's a bit unfortunate but we need the pthread dll here as libevent
  # gets compiled with pthread support once it is enabled in GCC, which we need
  # to do as otherwise the Rust compilaton breaks.
  mingwlibs=/var/tmp/dist/mingw-w64/[% c("arch") %]-w64-mingw32/bin/
  cp $zlibdir/lib/*.dll $openssldir/bin/*.dll $libeventdir/bin/*.dll \
      $gcclibs/*.dll $mingwlibs/libwinpthread-1.dll $distdir/Tor/
[% END %]

[% IF c("var/linux") %]
  mkdir -p "$distdir/Debug/Tor"
  cp $openssldir/lib/libssl.so.1.1 "$distdir/Tor/"
  cp $openssldir/lib/libcrypto.so.1.1 "$distdir/Tor/"
  cp $libeventdir/lib/libevent-2.1.so.7 "$distdir/Tor/"
  # We need to copy the libstdc++.so.6 for Tor Browser on older Linux distros.
  # Copying it into /Browser, which feels more natural, and amending
  # LD_LIBRARY_PATH breaks updates from a Tor Browser with the old
  # LD_LIBRARY_PATH value to the Tor Browser with the newer one. Thus, we copy
  # the libstdc++ into the directory with the libs tor depends on, too. See bug
  # 13359 for further details.
  libstdc_dir="$distdir/Tor/libstdc++"
  mkdir -p $libstdc_dir
  cp [% c("var/libpath") %]/libstdc++.so.6 $libstdc_dir/
  [% IF c("var/asan") -%]
    cp [% c("var/libpath") %]/libasan.so.6 "$distdir/Tor/"
    cp [% c("var/libpath") %]/libubsan.so.1 "$distdir/Tor/"
  [% END -%]
  [% IF c("var/linux-cross") %]
    cp -a /var/tmp/dist/gcc-cross/[% c("var/crosstarget") %]/lib/libssp.so* "$distdir/Tor/"
    # Required for libevent
    export LDFLAGS="$LDFLAGS -Wl,-rpath-link=/var/tmp/dist/gcc-cross/[% c("var/crosstarget") %]/lib"
    # Required for zlib
    export LDFLAGS="$LDFLAGS -L/usr/lib/[% c('var/crosstarget') %]"
    cp -a /usr/include/zconf.h /usr/include/zlib.h /var/tmp/dist/gcc-cross/[% c('var/crosstarget') %]/include

    TARGET_OBJCOPY=/var/tmp/dist/gcc-cross/bin/[% c("var/crosstarget") %]-objcopy
    TARGET_STRIP=/var/tmp/dist/gcc-cross/bin/[% c("var/crosstarget") %]-strip
  [% ELSE %]
    TARGET_OBJCOPY=objcopy
    TARGET_STRIP=strip
  [% END %]
  chmod 700 "$distdir"/Tor/*.so*
  chmod 700 $libstdc_dir/libstdc++.so.6
  $TARGET_STRIP $libstdc_dir/libstdc++.so.6
  # This is needed to make RPATH unavailable. See bug 9150.
  export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$distdir/Tor/"
[% END %]

[% IF c("var/osx") %]
  cp $libeventdir/lib/libevent-*.dylib $TORBINDIR/
[% END %]

cd /var/tmp/build/[% project %]-[% c('version') %]
# add git hash to micro-revision.i for #24995
echo '"[% c("abbrev", { abbrev_length => 16 }) %]"' > micro-revision.i
./autogen.sh
find -type f -print0 | xargs -0 [% c("var/touch") %]
./configure --disable-asciidoc --with-libevent-dir="$libeventdir" --with-openssl-dir="$openssldir" \
    [% IF c("var/asan") %]--enable-fragile-hardening[% END %] \
    [% IF c("var/windows") || c("var/android") %]--with-zlib-dir="$zlibdir"[% END %] \
    [% IF c("var/osx") %]--enable-static-openssl[% END %] \
    --prefix="$distdir" [% c("var/configure_opt") %]
[% IF c("var/osx") -%]
  export LD_PRELOAD=[% c("var/faketime_path") %]
  export FAKETIME="[% USE date; GET date.format(c('timestamp'), format = '%Y-%m-%d %H:%M:%S') %]"
[% END -%]
make -j[% c("buildconf/num_procs") %]
make install
[% IF c("var/osx") -%]
  unset LD_PRELOAD
[% END -%]
cp $distdir/share/tor/geoip $TORCONFIGDIR
cp $distdir/share/tor/geoip6 $TORCONFIGDIR

[% IF c("var/osx") %]
  LIBEVENT_FILE=`basename $libeventdir/lib/libevent-*.dylib`
  cd $distdir
  cp bin/tor $TORBINDIR/
  cd $TORBINDIR/
  x86_64-apple-darwin-install_name_tool -change $libeventdir/lib/$LIBEVENT_FILE @executable_path/$LIBEVENT_FILE tor
[% END %]

cd $distdir
[% IF c("var/windows") %]
  install -s $distdir/bin/tor.exe $distdir/Tor/
[% IF c("var/expert_bundle") %]
  install -s $distdir/bin/tor-gencert.exe $distdir/Tor/
[% END %]
[% END %]

[% IF c("var/linux") %]
  # Strip and generate debuginfo for libs
  $TARGET_OBJCOPY --only-keep-debug $distdir/bin/tor $distdir/Debug/Tor/tor
  install -s --strip-program="$TARGET_STRIP" $distdir/bin/tor $distdir/Tor/
  $TARGET_OBJCOPY --add-gnu-debuglink=./Debug/Tor/tor $distdir/Tor/tor
  for i in $distdir/Tor/*so*
  do
      LIB=`basename $i`
      $TARGET_OBJCOPY --only-keep-debug $distdir/Tor/$LIB $distdir/Debug/Tor/$LIB
      $TARGET_STRIP $distdir/Tor/$LIB
      $TARGET_OBJCOPY --add-gnu-debuglink=./Debug/Tor/$LIB $distdir/Tor/$LIB
  done
[% END %]

[% IF c("var/android") %]
  install -s --strip-program=[% c("var/cross_prefix") %]-strip $distdir/bin/tor $libsdir/libTor.so
[% END %]

mkdir -p [% dest_dir _ '/' _ c('filename') %]
[%
   IF c("var/expert_bundle");
     SET win_arch = c('var/windows-x86_64') ? 'win64' : 'win32';
     c('zip', {
        zip_src => [ 'Data', 'Tor', ],
        zip_args => dest_dir _ '/' _ c('filename') _ '/tor-' _ win_arch _ '-' _ c("version") _ '.zip',
      });
   ELSE;
     IF c("var/osx");
       SET tarsrcdir = [ 'Contents' ];
     ELSIF c("var/android");
       SET tarsrcdir = [ 'assets', 'jniLibs' ];
     ELSE;
       SET tarsrcdir = [ 'Data', 'Tor', ];
     END;
     c('tar', {
          tar_src => tarsrcdir,
          tar_args => '-czf ' _ dest_dir _ '/' _ c('filename') _ '/tor.tar.gz',
      });
   END;
%]

[% IF c("var/linux") %]
[% c('tar', {
        tar_src => [ 'Debug' ],
        tar_args => '-cJf ' _ dest_dir _ '/' _ c('filename') _ '/tor-debug.tar.xz',
    }) %]
[% END %]
